package z.explorer.ui.component.walker;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import java.util.*;

import z.explorer.Main;
import z.explorer.core.storage.*;
import z.explorer.core.storage.dao.ParamDAO;
import z.explorer.ui.common.ZTableModel;

public class ParamTable extends JTable {

	private TableRowSorter<ParamModel> sorter = null;

	private ParamModel model = new ParamModel();
	
	
	
	public ParamTable() {
		setModel(model);
		sorter = new TableRowSorter<ParamModel>(model);
		setRowSorter(sorter);
	}

	public ParamTable(ParamModel paramModel) {
		super(paramModel);
		model = paramModel;
		sorter = new TableRowSorter<ParamModel>(model);
		setRowSorter(sorter);
	}

	public ParamTable(String op) {
		model = new ParamModel(op);
		setModel(model);
		sorter = new TableRowSorter<ParamModel>(model);
		setRowSorter(sorter);
	}

	public void loadData(int tid,int eid){
		model.reloadData(tid, eid);
		model.fireTableDataChanged();
	}
	public void loadData(String op) {
		model.reloadData(op);
		model.fireTableDataChanged();
	}
	public ArrayList<Param> getParamList(){
		return model.getParamList();
	}
	public void getParamList(ArrayList<Param> list){
		model.setParamList(list);
	}
	public class ParamModel extends DefaultTableModel {

		private String op_str = "";

		private ArrayList<Param> paramList = new ArrayList<Param>();

		private final String columns[] = new String[] { "param_name",
				"param_value", "param_type" };

		public ParamModel() {
			try {
				analyzeParam();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public ParamModel(String op) {
			op_str = op;
			try {
				analyzeParam();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public ArrayList<Param> getParamList() {
			return paramList;
		}

		public void setParamList(ArrayList<Param> paramList) {
			this.paramList = paramList;
		}

		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return columns.length;
		}

		@Override
		public String getColumnName(int column) {
			// TODO Auto-generated method stub
			return columns[column];
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			// TODO Auto-generated method stub
			return String.class;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			if (paramList == null)
				return 0;
			else
				return paramList.size();
		}

		@Override
		public Object getValueAt(int row, int column) {
			// TODO Auto-generated method stub
			switch (column) {
			case 0:
				return paramList.get(row).getParam_name();
			case 1:
				return paramList.get(row).getParam_value();
			case 2:
				return paramList.get(row).getParam_type();
			default:
				return null;
			}
		}

		@Override
		public void setValueAt(Object aValue, int row, int column) {
			// TODO Auto-generated method stub
			String sValue = (String) aValue;
			switch (column) {
			case 0:
				paramList.get(row).setParam_name(sValue);
				break;
			case 1:
				paramList.get(row).setParam_value(sValue);
				break;
			case 2:
				paramList.get(row).setParam_type(sValue);
				break;
			default:
				return;
			}

		}

		@Override
		public boolean isCellEditable(int row, int column) {
			// TODO Auto-generated method stub
			if (column == 1 || column == 2) {
				return true;
			} else
				return false;
		}

		public void analyzeParam() throws Exception {
			int start_pos = -1;
			int end_pos = -1;
			String name;

			paramList.clear();

			while (true) {
				start_pos = end_pos + 1;
				start_pos = op_str.indexOf('#', start_pos);
				if (start_pos == -1) {
					return;
				}
				end_pos = op_str.indexOf('#', start_pos + 1);
				if (end_pos - start_pos <= 1)
					throw new Exception(
							"bad operation string with bad parameters");
				name = op_str.substring(start_pos + 1, end_pos);

				if (findByName(name) == paramList.size()) {
					paramList.add(new Param(name, "", "static"));
				}
			}
		}

		private int findByName(String name) {
			int index;
			for (index = 0; index < paramList.size(); index++) {
				if (paramList.get(index).getParam_name().equals(name))
					return index;
			}
			return index;
		}

		public void reloadData(String op) {
			if (op == null)
				op_str = "";
			else
				op_str = op;
			try {
				analyzeParam();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void reloadData(int table_id,int exe_seq){
			ParamDAO pDao = (ParamDAO) Main.context.getBean("paramDAO");
			java.util.List<Param> list = pDao.searchParamByExe(table_id, exe_seq);
			reloadData(list);
		}
		
		public void reloadData(List<Param> list) {
			if (list == null || list.size() <= 0) {
				reloadData("");
			} else {
				paramList.clear();
				for (Param p : list) {
					paramList.add(p);
				}
			}
		}
	}

}
